//! 配置测试

use crate::config::{BinanceConfig, BinanceConfigBuilder, CustomEndpoints};
use crate::{Environment, ProductType};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_creation() {
        let config = BinanceConfig::new(
            "test_api_key".to_string(),
            "test_secret_key".to_string(),
            ProductType::Spot,
        );

        assert_eq!(config.api_key, "test_api_key");
        assert_eq!(config.secret_key, "test_secret_key");
        assert_eq!(config.product_type, ProductType::Spot);
        assert_eq!(config.environment, Environment::Demo);
    }

    #[test]
    fn test_config_builder() {
        let config = BinanceConfigBuilder::new(
            "test_api_key".to_string(),
            "test_secret_key".to_string(),
            ProductType::UsdM,
        )
        .environment(Environment::Live)
        .timeout(60)
        .retry_count(5)
        .build();

        assert_eq!(config.api_key, "test_api_key");
        assert_eq!(config.secret_key, "test_secret_key");
        assert_eq!(config.product_type, ProductType::UsdM);
        assert_eq!(config.environment, Environment::Live);
        assert_eq!(config.timeout_seconds, 60);
        assert_eq!(config.retry_count, 5);
    }

    #[test]
    fn test_config_with_proxy() {
        let config = BinanceConfigBuilder::new(
            "test_api_key".to_string(),
            "test_secret_key".to_string(),
            ProductType::Spot,
        )
        .proxy(
            Some("http://127.0.0.1:8080".to_string()),
            Some("socks5://127.0.0.1:1080".to_string()),
        )
        .build();

        assert_eq!(config.http_proxy, Some("http://127.0.0.1:8080".to_string()));
        assert_eq!(config.ws_proxy, Some("socks5://127.0.0.1:1080".to_string()));
    }

    #[test]
    fn test_config_custom_endpoints() {
        let custom_endpoints = CustomEndpoints {
            rest_base_url: Some("https://custom-api.binance.com".to_string()),
            ws_base_url: Some("wss://custom-stream.binance.com".to_string()),
        };

        let config = BinanceConfigBuilder::new(
            "test_api_key".to_string(),
            "test_secret_key".to_string(),
            ProductType::Spot,
        )
        .custom_endpoints(custom_endpoints)
        .build();

        assert_eq!(
            config.rest_base_url(),
            "https://custom-api.binance.com"
        );
        assert_eq!(
            config.ws_base_url(),
            "wss://custom-stream.binance.com"
        );
    }

    #[test]
    fn test_product_type_urls() {
        // 测试现货URL
        assert_eq!(
            ProductType::Spot.rest_base_url(Environment::Live),
            "https://api.binance.com"
        );
        assert_eq!(
            ProductType::Spot.rest_base_url(Environment::Demo),
            "https://demo-api.binance.com"
        );

        // 测试USDT合约URL
        assert_eq!(
            ProductType::UsdM.rest_base_url(Environment::Live),
            "https://fapi.binance.com"
        );
        assert_eq!(
            ProductType::UsdM.rest_base_url(Environment::Demo),
            "https://demo-fapi.binance.com"
        );

        // 测试币本位合约URL
        assert_eq!(
            ProductType::CoinM.rest_base_url(Environment::Live),
            "https://dapi.binance.com"
        );
        assert_eq!(
            ProductType::CoinM.rest_base_url(Environment::Demo),
            "https://demo-dapi.binance.com"
        );

        // 测试期权URL
        assert_eq!(
            ProductType::Options.rest_base_url(Environment::Live),
            "https://eapi.binance.com"
        );
        assert_eq!(
            ProductType::Options.rest_base_url(Environment::Demo),
            "https://demo-api.binance.com"
        );
    }

    #[test]
    fn test_config_serialization() {
        let config = BinanceConfig::new(
            "test_api_key".to_string(),
            "test_secret_key".to_string(),
            ProductType::Spot,
        );

        // 测试序列化
        let serialized = serde_json::to_string(&config).unwrap();
        assert!(serialized.contains("test_api_key"));
        assert!(serialized.contains("test_secret_key"));

        // 测试反序列化
        let deserialized: BinanceConfig = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized.api_key, config.api_key);
        assert_eq!(deserialized.secret_key, config.secret_key);
        assert_eq!(deserialized.product_type, config.product_type);
    }

    #[test]
    fn test_config_file_operations() {
        let config = BinanceConfig::new(
            "test_api_key".to_string(),
            "test_secret_key".to_string(),
            ProductType::Spot,
        );

        // 测试保存到文件
        let result = config.save_to_file("test_config.toml");
        assert!(result.is_ok());

        // 测试从文件加载
        let loaded_config = BinanceConfig::from_file("test_config.toml");
        assert!(loaded_config.is_ok());

        let loaded = loaded_config.unwrap();
        assert_eq!(loaded.api_key, config.api_key);
        assert_eq!(loaded.secret_key, config.secret_key);
        assert_eq!(loaded.product_type, config.product_type);

        // 清理测试文件
        std::fs::remove_file("test_config.toml").unwrap();
    }
}
